package com.youtui.manager.realm;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import com.youtui.manager.model.Opter;
import com.youtui.manager.service.IOpterService;

public class ManagerRealm extends AuthorizingRealm {

	private @Autowired IOpterService opterService;
 
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)
			throws AuthenticationException {
		// 用户名不存在return null
		UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
		String username = token.getUsername();
		if (StringUtils.isBlank(username)) {
			return null;
		}

		Opter opter = null;
		try {
			opter = opterService.findOpterByLoginName(username);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		if (opter == null) {
			return null;
		}
		String password = opter.getPwd();
		AuthenticationInfo info = new SimpleAuthenticationInfo(opter, password, this.getClass().getSimpleName());
		return info;

	}

	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//		/* 加载权限 */
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); 
		return info; 
	}
}

